package wechat

import (
	"context"
	"fmt"
	"net/http"
	"product/common/nets"
)

// GetOAuth2AccessToken 微信第三方登录，code 换取 access_token
// appId: 应用唯一标识，在微信开放平台提交应用审核通过后获得
// appSecret: 应用密钥AppSecret，在微信开放平台提交应用审核通过后获得
// code: App用户换取access_token的code
// 文档：https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Development_Guide.html
func GetOAuth2AccessToken(ctx context.Context,
	appId, appSecret, code string) (accessToken *WeChatOAuth2Token,
	err error) {
	webReq := nets.NewWebHttpReq()
	response, err := webReq.SetContext(ctx).
		SetHeader("Content-Type", "application/json").
		SetMethod(http.MethodGet).
		SetUrl(WeChatAccessTokenUrl).
		SetUrlWithParams(map[string]interface{}{
			"appid":      appId,
			"secret":     appSecret,
			"code":       code,
			"grant_type": "authorization_code",
		}).
		Send()
	if err != nil {
		return nil, err
	}
	fmt.Println(webReq.URL.String())
	err = webReq.ParseResponseToJsonObj(response, accessToken)
	if err != nil {
		return nil, err
	}
	return accessToken, nil
}

// RefreshOAuth2AccessToken 刷新微信第三方登录后，获取到的 access_token
// appId：应用唯一标识，在微信开放平台提交应用审核通过后获得
// refreshToken：填写通过获取 access_token 获取到的 refresh_token 参数
// 文档：https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Development_Guide.html
func RefreshOAuth2AccessToken(ctx context.Context,
	appId, refreshToken string) (accessToken *WeChatOAuth2Token,
	err error) {
	webReq := nets.NewWebHttpReq()
	response, err := webReq.SetContext(ctx).
		SetHeader("Content-Type", "application/json").
		SetMethod(http.MethodGet).
		SetUrl(WeChatRefreshTokenUrl).
		SetUrlWithParams(map[string]interface{}{
			"appid":         appId,
			"grant_type":    "refresh_token",
			"refresh_token": refreshToken,
		}).
		Send()
	if err != nil {
		return nil, err
	}
	err = webReq.ParseResponseToJsonObj(response, &accessToken)
	if err != nil {
		return nil, err
	}
	return accessToken, nil
}

// CheckOAuth2AccessToken 检测 access_token 是否有效
// appId：应用唯一标识，在微信开放平台提交应用审核通过后获得
// accessToken：填写通过获取 access_token 获取到的 access_token 参数
// 文档：https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Authorized_API_call_UnionID.html
func CheckOAuth2AccessToken(ctx context.Context,
	accessToken, openId string) (result *WeChatCheckAccessTokenResp,
	err error) {
	webReq := nets.NewWebHttpReq()
	response, err := webReq.SetContext(ctx).
		SetHeader("Content-Type", "application/json").
		SetMethod(http.MethodGet).
		SetUrl(WeChatCheckAccessTokenUrl).
		SetUrlWithParams(map[string]interface{}{
			"access_token": accessToken,
			"openid":       openId,
		}).
		Send()
	if err != nil {
		return nil, err
	}
	err = webReq.ParseResponseToJsonObj(response, &result)
	if err != nil {
		return nil, err
	}
	return result, nil
}

// GetOAuth2UserInfo 获取用户信息
// accessToken：填写通过获取 access_token 获取到的 access_token 参数
// openId：填写用户的 openid
// 文档：https://developers.weixin.qq.com/doc/oplatform/Mobile_App/WeChat_Login/Authorized_API_call_UnionID.html
func GetOAuth2UserInfo(ctx context.Context,
	accessToken, openId string) (userInfo *WeChatOAuth2UserInfo,
	err error) {
	webReq := nets.NewWebHttpReq()
	response, err := webReq.SetContext(ctx).
		SetHeader("Content-Type", "application/json").
		SetMethod(http.MethodGet).
		SetUrl(WeChatUserInfoUrl).
		SetUrlWithParams(map[string]interface{}{
			"access_token": accessToken,
			"openid":       openId,
		}).
		Send()
	if err != nil {
		return nil, err
	}
	err = webReq.ParseResponseToJsonObj(response, &userInfo)
	if err != nil {
		return nil, err
	}
	return userInfo, nil
}
